// ignore_for_file: file_names, slash_for_doc_comments, must_call_super, unnecessary_overrides, override_on_non_overriding_member, must_be_immutable, prefer_const_constructors, sized_box_for_whitespace, avoid_unnecessary_containers, avoid_print

import 'package:flutter/material.dart';
import 'package:mybase/base/abstract/IFFR.dart';
import 'package:mybase/base/abstract/IFFRefresh.dart';
import 'package:mybase/base/abstract/IFFScaffold.dart';
import 'package:mybase/base/tool/color/FFColor.dart';
import 'package:mybase/base/widget/indicators/rectangular_indicator.dart';
import 'package:mybase/base/widget/layout/FFBranch.dart';
import 'package:mybase/base/widget/layout/FFImageButton.dart';
import 'package:mybase/base/widget/layout/FFLineContainer.dart';
import 'package:mybase/base/widget/layout/FFTextButton.dart';
import 'package:mybase/base/widget/page/FFScaffold.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TemplateTopTabbarPage extends FFScaffold {
  String? name;

  TemplateTopTabbarPage({
    Key? key,
    this.name,
  }) : super(key: key);

  @override
  TemplateTopTabbarPageState createState() => TemplateTopTabbarPageState();
}

class TemplateTopTabbarPageState extends FFScaffoldState with MFFScaffold {
  /**固定写法 */
  @override
  IFFScaffold get iffScaffold => this;

  @override
  TemplateTopTabbarPage get widget => super.widget as TemplateTopTabbarPage;

  @override
  IFFRefresh? get iffRefresh => refresh;
  late TemplateTopTabbarPageRefresh refresh =
      TemplateTopTabbarPageRefresh(state: this);

  /**固定写法end */

  ///tabbar 相关
  late TabController tabController;
  int currentIndex = 0;
  double tabbarHeight = 40.w;
  EdgeInsets tabbarMargin = EdgeInsets.only(top: 16.w, left: 16.w, right: 16.w);

  @override
  void ffInitState() {
    super.ffInitState();
  }

  @override
  Widget? ffNavi(BuildContext context) {
    // return AppNavi.createNavi(
    //   context,
    //   title: "标题",
    // );
  }

  @override
  Widget? ffBody(BuildContext context) {
    return Column(
      children: [
        tabbar(),
        Expanded(
          child: TabBarView(
            controller: tabController,
            children: tabbarChilds(),
          ),
        ),
      ],
    );
  }
}

extension TopPageStateTabbarUI on TemplateTopTabbarPageState {
  Widget tabbar() {
    return FFImageButton(
      heigth: tabbarHeight,
      margin: tabbarMargin,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(tabbarHeight * 0.5),
        border: Border.all(width: 1.w, color: rgba(139, 147, 137, 1)),
      ),
      borderRadius: BorderRadius.circular(tabbarHeight * 0.5),
      image: TabBar(
        controller: tabController,
        indicatorColor: Colors.green,
        indicatorWeight: 0.0001,
        tabs: tabsWidget(),
        labelPadding: EdgeInsets.zero,
        labelColor: rgba(226, 227, 221, 1),
        unselectedLabelColor: rgba(226, 227, 221, 1),
        indicator: RectangularIndicator(
          color: rgba(58, 75, 58, 1),
          bottomLeftRadius: 0,
          bottomRightRadius: 0,
          topLeftRadius: 0,
          topRightRadius: 0,
        ),
        onTap: (index) {
          setState(() {});
        },
      ),
    );
  }

  //数量
  void initTabbar(int count) {
    tabController = TabController(length: count, vsync: this);
    tabController.addListener(() {
      currentIndex = tabController.index;
      setState(() {});
    });
  }

  //自定义标题栏
  List<Widget> tabsWidget() {
    List<Widget> list = [];
    var tittls = ["test1", "test2", "test3"];

    for (int i = 0; i < tittls.length; i++) {
      list.add(
        FFLineContainer(
          direction: Axis.horizontal,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          decoration: BoxDecoration(
              border: Border(
                  left: BorderSide(width: 0.5.w, color: rgba(139, 147, 137, 1)),
                  right:
                      BorderSide(width: 0.5.w, color: rgba(139, 147, 137, 1)))),
          children: [
            FFBranch(
              trueWidget: FFImageButton(
                image: FFImageFactory.asset("assets/images/top/Icons.png"),
                width: 18.w,
                heigth: 18.w,
                margin: EdgeInsets.only(right: 10.w),
              ),
              condition: tabController.index == i,
            ),
            FFTextButton(
              text: tittls[i],
            ),
          ],
        ),
      );
    }

    return list;
  }

  //子页面
  List<Widget> tabbarChilds() {
    return <Widget>[
      Container(
        color: Colors.red,
      ),
      Container(
        color: Colors.white,
      ),
      Container(
        color: Colors.black,
      ),
    ];
  }
}

class TemplateTopTabbarPageRefresh with MFFRefresh {
  TemplateTopTabbarPageState state;

  TemplateTopTabbarPageRefresh({required this.state});

  @override
  ffRefresh(bool isHead) {
    ffRefreshResult(isHead, test, state);
  }

  //具体请求业务
  Future<IFFR> test(IRefreshParam? iRefreshParam) async {
    state.initTabbar(3);
    return R(rStatus: RStatus.result);
  }
}
